<template>
  <List ref="qmpList" v-model:finished="finished" :reset-fun="resetData" :load="load" :refresh="load">
    <Item
      v-for="(item, i) in list"
      :key="i"
      :item-data="item"
      :dictData="dictData"
      @detail="toDetail(item)"
      @marketChange="onMarketChange(item)"
      @edit="toEdit(item)"
      @stop="onStop(item)"
      @handover="toHandover(item)"
    />
  </List>
</template>

<script lang="ts">
  import { ref, reactive, toRefs, defineComponent, watch } from 'vue'
  import { useRouter } from 'vue-router'
  import List from '@/components/List/index.vue'
  import Item from './Item.vue'
  import { geList, detailItem } from '@/apis/attractInvestment/potentialCustomerFollowUp'
  import { getDictByPatchType } from '@/apis/dataDict'

  interface datasModle {
    list: any[]
    finished: boolean
    dictData: any
  }
  export default defineComponent({
    name: 'FollowUPList',
    components: { List, Item },
    props: {
      searchValue: {
        type: String,
        default: ''
      },
      type: {
        type: String,
        default: ''
      }
    },
    setup(props, ctx) {
      const qmpList = ref()
      const $router = useRouter()
      const datas = reactive<datasModle>({
        list: [],
        finished: false,
        dictData: {}
      })
      const load = async (page: number, rows: number) => {
        const filtersRaw: object[] = []
        if (props.searchValue) {
          filtersRaw.push({
            id: '',
            property: '[customerName,customerPhone,marketName,managerName]',
            operator: 'OR LIKE',
            value: props.searchValue
          })
        }
        const paramsData = {
          page,
          rows,
          sort: 'joinStatus',
          order: 'asc',
          filtersRaw,
          paramsRaw: {},
          sortersRaw: [
            { id: 'joinStatus', property: 'joinStatus', direction: 'asc' },
            { id: 'allotTime', property: 'allotTime', direction: 'desc' }
          ],
          pageFlag: true
        }
        return geList(paramsData).then((res: any) => {
          const resData = res.data
          const list = resData.records
          if (page === 1) {
            datas.list = list
          } else {
            datas.list = datas.list.concat(list)
          }
          if (resData.total <= datas.list.length) {
            datas.finished = true
          }
        })
      }
      const resetData = () => {
        datas.list = []
      }
      const resetPageData = () => {
        qmpList.value.resetPageData()
      }

      // 刷新列表中某个数据的值
      const refreshItem = (id: string) => {
        const flag = datas.list.some((v) => v.id === id)
        if (!flag) return

        detailItem(id).then((res: any) => {
          const d = res.data
          if (d && d.id) {
            datas.list = datas.list.map((item: any) => {
              if (item.id === id) {
                item = { ...res.data }
              }
              return item
            })
          }
        })
      }
      // 获取数据字典
      const getDictData = async () => {
        const dictTypeCodes = ['CTM_INTENTION_LEVEL', 'CTM_JOIN_IN_STATE', 'TOBE_MARKET_TYPE']
        datas.dictData = await getDictByPatchType({ dictTypeCodes })
      }

      getDictData()

      const toDetail = (row: any) => {
        $router.push({
          path: '/potentialCustomerFollowUp/detail',
          query: {
            id: row.id
          }
        })
      }
      const onMarketChange = (row: any) => {
        $router.push({
          name: 'PotentialCustomerMarketChange',
          query: {
            id: row.intendedCustomerId // 意向客户ID
          },
          params: {
            customerName: row.customerName,
            customerPhone: row.customerPhone,
            marketName: row.marketName
          }
        })
      }

      const toEdit = (row: any) => {
        $router.push({
          path: '/potentialCustomerFollowUp/edit',
          query: {
            id: row.id
          }
        })
      }

      // 终止
      const onStop = (row: any) => {
        console.log('终止')
        ctx.emit('stop', row)
      }

      // 交接
      const toHandover = (row: any) => {
        $router.push({
          path: '/potentialCustomerFollowUp/handover',
          query: {
            id: row.id,
            type: 'handover'
          }
        })
      }

      watch(
        () => props.searchValue,
        () => {
          resetPageData()
        }
      )
      return {
        ...toRefs(datas),
        load,
        resetData,
        qmpList,
        resetPageData,
        toDetail,
        onMarketChange,
        toEdit,
        onStop,
        toHandover,
        refreshItem
      }
    }
  })
</script>
<style lang="less" scoped></style>
